//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true
    },
    {
        id: 'name',
        title: '银行名称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'insideCode',
        title: '银行编码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true
    },
    {
        id: 'icon',
        title: '图标',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'operation',
        title: '操作',
        headerClass: 'text-center width-300',
        columnClass: 'text-center width-300',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var edit, del, html;
            // 判断是否有编辑权限
            if ($("#editBank") && $("#editBank").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }
            // 判断是否有删除权限
            if ($("#delBank") && $("#delBank").val() == 1) {
                del = "<a href='javascript:;' class='btn btn-danger btn-sm' onclick='OperateHandle.delBank(" + record.id + ")' ><i class='fa fa fa-lg fa-trash-o'></i>&nbsp;删除&nbsp;</a>"
            }
            html = edit + del;
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'bank/json/list',
    exportFileName: '银行列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id", "sortNo"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);

// 默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
//排序
grid.sortParameter.columnId = 'sortNo';
grid.sortParameter.sortType = 0;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        /**
         * 图片缩放
         */
        function _refreshImage() {
            $(".viewImage").jqthumb({
                width: 180,
                height: 100,
                after: function (imgObj) {
                    imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
                }
            });
        }

        //新增界面图片上传
        $("#addFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicPath').val(data.result.data);
                    $('#addFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        //编辑界面图片上传
        $("#editFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicPath').val(data.result.data);
                    $('#editFormPicImg').attr('src', ncGlobal.fileRoot +data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();
            $addForm = $("#addForm");
            $addForm.find("input[name='name']").val("");
            $addForm.find("input[name='insideCode']").val("");
            $addForm.find("input[name='sortNo']").val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot+"default_image.gif");
            _refreshImage();
        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            $editForm = $("#editForm");
            $editForm.find('input[name="id"]').val(gridData.id);
            $editForm.find('input[name="name"]').val(gridData.name);
            $editForm.find('input[name="insideCode"]').val(gridData.insideCode);
            $editForm.find('input[name="sortNo"]').val(gridData.sortNo);
            $('#editFormPicPath').val(gridData.icon);
            $('#editFormPicImg').attr('src', ncGlobal.fileRoot+gridData.icon);
            _refreshImage();
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_name_or_lk_inside_code'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        delBank: _delBank
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});